import { Component, OnInit, ViewChild, ElementRef, Injector } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { WarehouseDTO, ServiceProxy, SiteDTO, PathDTO } from '@shared/service-proxy/service-proxies';
import { NzTableHelper } from '@shared/helpers/NzTableHelper';
import { finalize } from 'rxjs/operators';
import { loadModules } from 'esri-loader';
import * as moment from 'moment';
import { HttpHeaders, HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-path-alarm',
  templateUrl: './path-alarm.component.html',
  styleUrls: ['./path-alarm.component.less']
})
export class PathAlarmComponent extends AppComponentBase implements OnInit {

  sites: SiteDTO[] = [];
  paths: PathDTO[] = [];
  pathAll: PathDTO[] = [];
  siteId: string = '';
  siteId2: string = '';
  routeId: string = '';
  theTime: moment.Moment[] = [];
  currentUser = this.appSession.user;

  constructor(
    injector: Injector,
    private _service: ServiceProxy,
    private httpClient: HttpClient) {
    super(injector);
  }

  ngOnInit() {
    this.getSites();
    this.getPaths();
    this.getPathAlarm();
  }

  reset() {
    this.siteId = '';
    this.siteId2 = '';
    this.routeId = '';
    this.theTime = [];
    this.getPathAlarm();
  }
  exportToExcel(): void {
    const nameValues: abp.INameValue[] = [];
    nameValues.push({ name: 'routeId', value: this.routeId });
    nameValues.push({ name: 'startTime', value: this.theTime[0] });
    nameValues.push({ name: 'endTime', value: this.theTime[1] });
    nameValues.push({ name: 'launchsiteId', value: this.siteId });
    nameValues.push({ name: 'destinationsiteId', value: this.siteId2 });

    const queryString = abp.utils.buildQueryString(nameValues);
    const exportUrl = this.serviceIpUrl + '/api/center/statistics/route/warntrendexport' + queryString; // 以上为导出url的拼装，可自行拼装，直接一行拼起来也行

    const options: any = {
      responseType: 'blob',
      headers: new HttpHeaders({ 'Content-Type': 'application/json', 'Accept': 'application/json' })
    };

    this.httpClient.get(exportUrl, options).subscribe(res => {
      const blob = new Blob([res], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });// excel专用，其他请自行查阅资料
      const objectUrl = URL.createObjectURL(blob);
      const a = document.createElement('a');
      document.body.appendChild(a);
      a.setAttribute('style', 'display:none');
      a.setAttribute('href', objectUrl);
      const fileName = this.l('path_warn_statistics') + moment().format('YYYYMMDDHHmmss') + '.xlsx';
      a.setAttribute('download', fileName);
      a.click();
      URL.revokeObjectURL(objectUrl);
    });
  }

  getSites() {
    this._service.getAllSitesUsingGET().subscribe(result => {
      this.sites = result.result.fieldList;
    });
  }

  getPaths() {
    this._service.getAllPathUsingGET().subscribe(result => {
      this.paths = result.result.fieldList;
      this.pathAll = result.result.fieldList;
    });
  }

  getPathList() {
    debugger;
    if (this.siteId && this.siteId2) {
      this.paths = this.pathAll.filter(m => m.startSiteId == this.siteId && m.endSiteId == this.siteId2);
    }
    else if (this.siteId) {
      this.paths = this.pathAll.filter(m => m.startSiteId == this.siteId);
    }
    else if (this.siteId2) {
      this.paths = this.pathAll.filter(m => m.endSiteId == this.siteId2);
    }
    else{
      this.paths = this.pathAll;
    }
  }



  getPathAlarm() {
    this.single = [
    ];
    if (this.currentUser.siteId) {
      this.siteId = this.currentUser.siteId;
    }
    this._service.warnTrendUsingGET(this.siteId, this.theTime[0], this.theTime[1], this.siteId2, this.routeId).subscribe(result => {
      const warnList = result.result.filter(m => m.warnType != null);

      this.single = [{ name: this.l('warn_disassemble_bonnet'), value: 0 },
      { name: this.l('warn_lever_shearing'), value: 0 },
      { name: this.l('warn_lvd'), value: 0 },
      { name: this.l('warn_overspeed'), value: 0 },
      { name: this.l('warn_gps_lost'), value: 0 },
      { name: this.l('warn_long_time'), value: 0 },
      { name: this.l('warn_route_deviation'), value: 0 },
      { name: this.l('warn_park_long_time'), value: 0 }]

      warnList.map(v => {
        var listType = this.single.find(m => m.name == v.warnType);
        if (listType)
          listType.value = v.frequency
      });
      this.single = this.single;
      console.log(this.single)
    })
  }

  single: any[] = [];

  view: any[] = [1000, 500];

  // options
  showXAxis = true;
  showYAxis = true;
  gradient = false;
  showLegend = true;
  showXAxisLabel = true;
  xAxisLabel = this.l("warn_type");
  showYAxisLabel = true;
  yAxisLabel = this.l("times");
  barPadding = 30;
  colorScheme = {
    domain: ["#5AA454", "#A10A28", "#C7B42C", "#AAAAAA"]
  };

  onSelect(event) {
    console.log(event);
  }

}
